<?php
// +----------------------------------------------------------------------
// | 专利管理（专利添加、修改、删除等操作）
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-12-2 下午3:04:44
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');

class Patent extends MY_Controller{
	
	private $operation = array('down', 'delete');
	
	
	/**
	 * 专利管理首页
	 * @author Baip
	 */
	public function index(){
		$where = array();
		//搜索
		$search_name = $this->input->get_post('search_name');
		if ( !empty( $search_name ) ){
			$where = "title LIKE '%$search_name%'" ;
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}
		//载入商标属性模型
		$this->load->model('Patent_model','patent');
		$total = $this->patent->getCount('');//获取数据记录数
		//分页
		$this->load->library('pagination');//载入分页类
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('patent/index'),$total, $page_size, 3, $suffix, site_url('/patent/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="id, title, service_price, office_type, office_price, addtime, state,recommend";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->patent->getDataObj($where, $field, $page_size, $offset);//获取数据
		//载入分类模型
		$this->load->model('Catgory_model', 'catgory');
		foreach ( $list as $key => $val ){
			$office_price_obj = '';
			$office_price_arr = unserialize( $val->office_price );
			$catgory_obj = $this->catgory->getField(array("id" => explode(',', $val->office_type)), 'id,title');
			$list[$key]->catgory = $catgory_obj;
		}
		$pages = $this->pagination->create_links();//显示分页
		
		$this->load->vars('list', $list);//数据
		$this->load->vars('record_count',$total);
		$this->load->vars('pages', $pages);//分页输出
		
		$this->load->vars('meta_title', '专利管理');
		$this->load->view('patent/index');
	}
	
	/**
	 * 添加专利产品
	 * @author Baip
	 */
	public function add(){
		//引入表单验证类
		$this->load->library('form_validation');
		if ( IS_POST && $this->form_validation->run('patent') ){
			//载入专利模型
			$this->load->model('Patent_model', 'patent');
			$insert_id = $this->patent->addData();
			if ( intval( $insert_id ) > 0 ){
				$this->show_message('success', '操作成功', site_url('patent/index'));
			}else{
				$this->show_message('error', $this->patent->getError(), site_url('patent/add'));
			}
			
			
		}else{
			//获取分类信息
			$catgory = $this->getCatgorytoTree( array('hide'=>'0', 'pid'=>159), false );
			$catgory_patent_type = $this->getCatgorytoTree( array('hide'=>'0', 'pid'=>'173'), false );
			
			$this->load->vars('meta_title','添加专利产品');
			$this->load->vars('catgory', $catgory);
			$this->load->vars('catgory_patent_type', $catgory_patent_type);
			$this->load->view('patent/add');
		}	
	}
	
	/**
	 * 修改专利产品
	 */
	public function modfiy(){
		$id = $this->uri->segment(3, 0);//获取id
		if ( !is_numeric( $id ) ){
			$this->show_message('error', '参数错误');
		}
		//载入专利模型
		$this->load->model('Patent_model', 'patent');
		//引入表单验证类
		$this->load->library('form_validation');
		if ( IS_POST && $this->form_validation->run('patent') ){
			//载入专利模型
			$this->load->model('Patent_model', 'patent');
			$insert_id = $this->patent->updateData( array('id'=>$id) );
			if ( intval( $insert_id ) > 0 ){
				$this->show_message('success', '操作成功', site_url('patent/index'));
			}else{
				$this->show_message('error', $this->patent->getError(), site_url('patent/add'));
			}
		}else{
			$info = $this->patent->getFind(array('id'=>$id),'*');
			//获取分类信息
			$catgory = $this->getCatgorytoTree( array('hide'=>'0', 'pid'=>159), false );
			$catgory_patent_type = $this->getCatgorytoTree( array('hide'=>'0', 'pid'=>'173'), false );
			$catgory = changeArr( $catgory, 'id' ) ;//转换数组下标
			$this->load->vars('meta_title','修改专利产品');
			$this->load->vars('catgory', $catgory);
			$this->load->vars('catgory_patent_type', $catgory_patent_type);
			$this->load->vars('info', $info);
			$this->load->view('patent/add');
		}
	}
	
	
	
	
	/**
	 * 修改状态（下架，上架）
	 */
	public function setstate(){
		$id    = $this->input->get_post('id',true);
		$state = $this->input->get_post('state', true);
		$state = empty( $state ) ? 0 : $state;
		if ( !is_array( $id ) ){
			if ( 0 == $id || !is_numeric($id) ){
				$this->show_message('error', '参数错误', site_url('trademark/index') );
			}else{
				$map = array('id'=>$id);
			}
		}else{
			$map = array('id'=> $id);
		}	
		//加载模型　
		$this->load->model('Patent_model', 'patent');
		$status = $this->patent->updateState( $map, array('state'=> $state) );
		if ( $status ){
			ajaxreturn(1,'操作成功', site_url('patent/index'));
		}else{
			ajaxreturn(1,'操作成功', site_url('patent/index'));
		}
	}
}